import random

import pygame
from pygame.sprite import Sprite


class Alien(Sprite):
    """ 外星人 """

    def __init__(self, ai_game):
        super().__init__()
        # 记录界面尺寸
        self.screen = ai_game.screen
        self.settings = ai_game.settings

        # 加载外星人图片
        self.image = pygame.transform.scale(pygame.image.load('images/alien.bmp'), (70, 70))
        self.rect = self.image.get_rect()

        # 外星人统一左上角出现
        self.rect.x = self.rect.width
        self.rect.y = -self.rect.bottom

        self.y = float(self.rect.y)
        self.x = float(self.rect.x)
        # 外星人移速
        self.speed_y = random.uniform(0.5, self.settings.alien_speed)
        self.speed_x = random.uniform(0.4, self.settings.alien_speed)
        # 外星人移动方向
        random_num = random.uniform(0, 1)
        if random_num >= 0.5:
            self.direction = self.settings.fleet_direction
        else:
            self.direction = -self.settings.fleet_direction

    def update(self):
        self.x += (self.speed_x * self.direction)
        self.y += self.speed_y
        self.rect.x = self.x
        self.rect.y = self.y
